package com.spring.activity;

import android.app.DatePickerDialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.DatePicker;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.spring.gowhere.R;
import com.spring.httputils.RestClient;
import com.spring.model.ReturnAccountInfo;
import com.spring.util.Constant;
import com.spring.util.DialogUtil;
import com.spring.widget.DialogDatePicker;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Created by Administrator on 2016/8/30 0030.
 */
public class LeaveActivity extends BaseActivity  implements View.OnClickListener{
    RestClient restClient = null;

    ReturnAccountInfo accountinfo = null;
    String username = null;
    String logintype = null;
    private TextView etStartTime, etEndTime;
    private Button btnquery;
    private LinearLayout linearlayout01;
    String starttime;
    String endtime;
    private Date mStartDate;
    private Date mEndDate;
    private  RadioButton rb1,rb2;
    private  RelativeLayout stn,sick;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.myxflist1);
        linearlayout01 = (LinearLayout) findViewById(R.id.linearlayout01);
        etStartTime = (TextView) findViewById(R.id.querykq);
        etEndTime = (TextView) findViewById(R.id.querykqendtime);
        btnquery = (Button) findViewById(R.id.btnlogin);
        rb1 = (RadioButton) findViewById(R.id.rb1);
        rb2 = (RadioButton) findViewById(R.id.rb2);
        SharedPreferences userInfo = getSharedPreferences("user_info", 0);
        username = userInfo.getString("username", "");
        logintype = userInfo.getString("logintype", "");

        etStartTime.setOnClickListener(this);
        etEndTime.setOnClickListener(this);
        stn = (RelativeLayout) findViewById(R.id.stn);
        sick= (RelativeLayout) findViewById(R.id.sick);
        stn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rb1.setChecked(true);
                if (rb1.isChecked()) {
                    rb2.setChecked(false);
                } else {
                    rb2.setChecked(true);
                }

            }
        });
        sick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rb2.setChecked(true);
                if (rb2.isChecked()) {
                    rb1.setChecked(false);
                } else {
                    rb1.setChecked(true);
                }

            }
        });
        rb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (rb1.isChecked()) {
                    rb2.setChecked(false);
                } else {
                    rb2.setChecked(true);
                }

            }
        });
        rb2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (rb2.isChecked()) {
                    rb1.setChecked(false);
                } else {
                    rb1.setChecked(true);
                }
            }
        });
        ImageButton imageButton = (ImageButton) findViewById(R.id.title_left_ib);
        imageButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });

        Calendar c = Calendar.getInstance();
        mStartDate = c.getTime();
        mEndDate = c.getTime();
        SimpleDateFormat df = new SimpleDateFormat(
                "yyyy-MM-dd", Locale.UK);
        starttime = df.format(mStartDate);
        endtime = df.format(mEndDate);



        etStartTime.setText("开始时间："+starttime);
        etEndTime.setText("结束时间："+endtime);

        TextView textView = (TextView) findViewById(R.id.title_tv);
        textView.setText("请假");
        boolean islianwang = new Constant().isConnect(LeaveActivity.this);
        btnquery.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if(!new Constant().isConnect(LeaveActivity.this)){
                    return;
                }

                if(mStartDate==null){
                    DialogUtil.showHintDialog(LeaveActivity.this, "开始日期不能为空");
                    return;
                }
                if(mEndDate==null){
                    DialogUtil.showHintDialog(LeaveActivity.this,"结束日期不能为空");
                    return;
                }
                int day = (int)((mEndDate.getTime()-mStartDate.getTime())/(1000*3600*24));
                if(day<0){
                    DialogUtil.showHintDialog(LeaveActivity.this, "结束日期必须大于开始日期");
                } else if(day>30){
                    DialogUtil.showHintDialog(LeaveActivity.this,"查询日期范围不能超过30天");
                }
                Toast.makeText(LeaveActivity.this,"恭喜您,请假成功",Toast.LENGTH_SHORT).show();
            }
        });




    }
    private void onSelectStartDate(){
        final Calendar cal = Calendar.getInstance();
        DialogDatePicker datePicker = new DialogDatePicker(LeaveActivity.this,
                new DatePickerDialog.OnDateSetListener() {

                    @Override
                    public void onDateSet(DatePicker view, int year,
                                          int monthOfYear, int dayOfMonth) {
                        cal.set(Calendar.YEAR, year);
                        cal.set(Calendar.MONTH, monthOfYear);
                        cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                        SimpleDateFormat df = new SimpleDateFormat(
                                "yyyy-MM-dd", Locale.UK);
                        mStartDate = cal.getTime();
                        etStartTime.setText("开始时间："+df.format(cal.getTime()));
                    }
                }, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
                cal.get(Calendar.DAY_OF_MONTH));
        datePicker.getDatePicker().setCalendarViewShown(false);
        Window window = datePicker.getWindow();
        window.setGravity(Gravity.CENTER);
        window.setWindowAnimations(R.style.DialogstyleLeft);
        datePicker.show();
    }

    private void onSelectEndDate(){
        final Calendar cal = Calendar.getInstance();
        DialogDatePicker datePicker = new DialogDatePicker(LeaveActivity.this,
                new DatePickerDialog.OnDateSetListener() {

                    @Override
                    public void onDateSet(DatePicker view, int year,
                                          int monthOfYear, int dayOfMonth) {
                        cal.set(Calendar.YEAR, year);
                        cal.set(Calendar.MONTH, monthOfYear);
                        cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                        SimpleDateFormat df = new SimpleDateFormat(
                                "yyyy-MM-dd", Locale.UK);
                        mEndDate = cal.getTime();
                        etEndTime.setText("结束时间：" +df.format(cal.getTime()));
                    }
                }, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
                cal.get(Calendar.DAY_OF_MONTH));
        datePicker.getDatePicker().setCalendarViewShown(false);
        Window window = datePicker.getWindow();
        window.setGravity(Gravity.CENTER);
        window.setWindowAnimations(R.style.DialogstyleLeft);
        datePicker.show();
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.querykq:
                onSelectStartDate();
                break;
            case R.id.querykqendtime:
                onSelectEndDate();
                break;
            default:
                break;
        }
    }


}
